package com.pirestupppai.ui.activity;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.pirestupppai.R;
import com.pirestupppai.adapter.MessageListAdapter;
import com.pirestupppai.base.BaseActivity;
import com.pirestupppai.base.BaseAdapter;
import com.pirestupppai.base.BaseStatus;
import com.pirestupppai.bean.TupaiMsg;
import com.pirestupppai.network.NetworkRequest;
import com.pirestupppai.network.TupaiSubscriber;
import com.pirestupppai.widgets.view.CustomRecyclerView;
import com.pirestupppai.widgets.view.LoadMoreFooter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 消息列表
 *
 * @author: xiaoluo
 * @date: 2017-04-27 10:38
 */
public class MessageListActivity extends BaseActivity{

    @BindView(R.id.msg_recycler_view)
    CustomRecyclerView recyclerView;

    private LoadMoreFooter mFooterView;
    private MessageListAdapter mAdapter;
    private LinearLayoutManager mLinearLayoutManager;

    private int mPosition = 0;  // 列表最后一位
    private int mPage = 1;
    private int mSize = 15;
    private boolean hasMore = true;
    private List<TupaiMsg> mMsgList = new ArrayList<>();

    @Override
    protected Context getActivityContext() {
        return this;
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.activity_message_list;
    }

    @Override
    protected void initViews() {
        mFooterView = new LoadMoreFooter(mContext);
        mAdapter = new MessageListAdapter(mContext, mMsgList);
        mAdapter.addFooterView(mFooterView);
        mLinearLayoutManager = new LinearLayoutManager(mContext);
        mLinearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);

        recyclerView.setEmptyText("暂时没有任何消息哟");
        recyclerView.getRecyclerView().setLayoutManager(mLinearLayoutManager);
        recyclerView.getRecyclerView().setAdapter(mAdapter);
        recyclerView.getRecyclerView().setHasFixedSize(true);
    }

    @Override
    protected void initListeners() {
// 监听到达底部事件
        recyclerView.getRecyclerView().addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                //到达底部时,加载更多
                int lastVisiblePosition = mLinearLayoutManager.findLastCompletelyVisibleItemPosition() + 1;
                if (mLinearLayoutManager.getChildCount() > 0
                        && mLinearLayoutManager.getItemCount() >= mLinearLayoutManager.getChildCount()
                        && lastVisiblePosition >= mLinearLayoutManager.getItemCount()
                        && dy > 0) {
                    mPosition = lastVisiblePosition;

                    if (hasMore) {
                        mFooterView.setStatus(LoadMoreFooter.Status.LOADING);
                        getData(BaseStatus.LOADMORE);
                    }
                }
            }
        });

        mAdapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View v, int position) {

            }
        });
    }

    @Override
    protected void initLogic() {
        getData(BaseStatus.REFRESH);
    }

    @Override
    public void onClick(View v) {

    }

    /**
     * 获取数据
     */
    private void getData(final BaseStatus status) {
        if (status == BaseStatus.REFRESH) {
            mFooterView.setStatus(LoadMoreFooter.Status.GONE);
            hasMore = true;
            mPage = 1;
        } else {
            mPage++;
        }

        NetworkRequest.getInstance().getMessageList(mPage, mSize, this,
                new TupaiSubscriber<List<TupaiMsg>>(mContext) {
                    @Override
                    protected void requestSuccess(List<TupaiMsg> tupaiMsgs) {
                        if (tupaiMsgs != null && tupaiMsgs.size() != 0) {
                            if (status == BaseStatus.REFRESH) {
                                mMsgList.clear();
                                mMsgList.addAll(tupaiMsgs);
                                mAdapter.notifyDataSetChanged();
                            } else {
                                mMsgList.addAll(tupaiMsgs);
                                mAdapter.notifyItemInserted(mPosition);
                            }
                        } else if (status == BaseStatus.REFRESH) {
                            recyclerView.setEmpty();
                        } else {
                            mFooterView.setStatus(LoadMoreFooter.Status.THE_END);
                        }

                        if (tupaiMsgs != null && tupaiMsgs.size() < mSize) {
                            mFooterView.setStatus(LoadMoreFooter.Status.THE_END);
                            hasMore = false;
                        }
                    }

                    @Override
                    protected void requestError(String error) {
                        if (status == BaseStatus.LOADMORE) {
                            mFooterView.setStatus(LoadMoreFooter.Status.GONE);
                            mPage--;
                        }
                    }
                });
    }
}
